<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright  Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Coupon codes grid
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Silk_Zgb_Block_Adminhtml_Notify_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('notifyGrid');
        $this->setUseAjax(true);
    }

    /**
     * Prepare collection for grid
     *
     * @return Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareCollection()
    {

        $session = Mage::getSingleton('admin/session');
        $adminId = $session->getUser()->getId();
        $websiteId = $session->getUser()->getWebsiteId();
        if(!$websiteId){
            echo "你是平台管理员，请到站点管理中查看当前站点库存预警";
            return false;
        }
        $zgb = Mage::helper('zgb');
        $storeId = 0;
        $ids = array();
        // $warehouse = null;

        //仓库
        // if($roleId == $zgb->getRoleId('warehouse'))
        // {
        //     $warehouse = Mage::getModel('zgb/warehouse')->loadByAdminId($adminId);
        //     $ids = $warehouse->getProductIds();
        //     $websiteId = $warehouse->getWebsiteId();
        //     if($websiteId)
        //     {
        //         $storeId = Mage::getModel('core/website')->load($websiteId)->getStoreCollection()->getFirstItem()->getId();
        //     }
        //     //echo $warehouse->getId();
        // }


        if($websiteId)
        {
            $storeId = Mage::getModel('core/website')->load($websiteId)->getDefaultStore()->getId();
        }

        $collection = Mage::getResourceModel('zgb/notify_collection');
        $select = $collection->getSelect();

        $name  = Mage::getResourceModel('catalog/product')->getAttribute('name');
        $tablename = $name->getBackend()->getTable();
        $attributeId = $name->getAttributeId();

        $select->joinLeft(array('pn' => $tablename), "pn.entity_id=main_table.product_id and pn.store_id=0 and pn.attribute_id='{$attributeId}'", array('name'=>'pn.value'));

        $status  = Mage::getResourceModel('catalog/product')->getAttribute('status');
        $tablename = $status->getBackend()->getTable();
        $attributeId = $status->getAttributeId();

        if($storeId)
        {
            $select->joinLeft(array('ps' => $tablename), "ps.entity_id=main_table.product_id and ps.store_id={$storeId} and ps.attribute_id='{$attributeId}'", array('status'=>'ps.value'));
        }
        //else
        //{
        //    //$select->joinLeft(array('ps' => $tablename), "ps.entity_id=main_table.product_id and ps.attribute_id='{$attributeId}'", array('status'=>'ps.value'));
        //}

        $select->joinLeft(array('psku' => 'catalog_product_entity'), "psku.entity_id=main_table.product_id", array('sku'=>'psku.sku'));

        $collection->addFilterToMap('name', 'pn.value');
        $collection->addFilterToMap('sku', 'psku.sku');

        
        if($websiteId)
        {
            $collection->addFieldToFilter('website_id', array('eq'=>$websiteId));
        }
        
        if($storeId)
        {
            $collection->addFieldToFilter('ps.value', array('eq'=>1));
        }
        
        $collection->addFieldToFilter('low_stock_date', array('notnull'=>true));
        $collection->setOrder('low_stock_date','desc');
        //$collection->addAttributeToFilter('sku',array('eq'=>'KSF019'));
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    /**
     * Define grid columns
     *
     * @return Mage_Adminhtml_Block_Widget_Grid
     */
    protected function _prepareColumns()
    {

        $this->addColumn('name', array(
            'header' => Mage::helper('zgb')->__('Name'),
            'index'  => 'name',
        ));

        $this->addColumn('sku', array(
            'header' => Mage::helper('zgb')->__('SKU'),
            'index'  => 'sku',
        ));

        $this->addColumn('qty', array(
            'header' => Mage::helper('zgb')->__('Qty'),
            'index'  => 'qty',
            'width' => '10'
        ));


        $this->addColumn('low_stock_date', array(
            'header' => Mage::helper('zgb')->__('预警时间'),
            'index'  => 'low_stock_date',
            'type'   => 'datetime',
            'align'  => 'center',
            'width'  => '200'
        ));

        //$this->addExportType('*/*/exportCouponsCsv', Mage::helper('customer')->__('CSV'));
        //$this->addExportType('*/*/exportCouponsXml', Mage::helper('customer')->__('Excel XML'));
        return parent::_prepareColumns();
    }

    /**
     * Configure grid mass actions
     *
     * @return Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Coupons_Grid
     */
    protected function _prepareMassaction()
    {
        // $this->setMassactionIdField('coupon_id');
        // $this->getMassactionBlock()->setFormFieldName('ids');
        // $this->getMassactionBlock()->setUseAjax(true);
        // $this->getMassactionBlock()->setHideFormElement(true);

        // $this->getMassactionBlock()->addItem('delete', array(
        //      'label'=> Mage::helper('adminhtml')->__('Delete'),
        //      'url'  => $this->getUrl('*/*/couponsMassDelete', array('_current' => true)),
        //      'confirm' => Mage::helper('salesrule')->__('Are you sure you want to delete the selected coupon(s)?'),
        //      'complete' => 'refreshCouponCodesGrid'
        // ));

        return $this;
    }

    /**
     * Get grid url
     *
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('admin_zgb/adminhtml_inventory/notifyGrid', array('_current'=> true));
    }
}
